package com.sfpay.coreplatform.order.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sfpay.coreplatform.account.valueobject.dto.TallySerialQuery;
import com.sfpay.coreplatform.account.valueobject.tmo.TallySerial;
import com.sfpay.coreplatform.order.common.OrderResponseCode;
import com.sfpay.coreplatform.order.persistence.dao.IOrderQueryDao;
import com.sfpay.coreplatform.order.service.IOrderQueryService;
import com.sfpay.coreplatform.order.service.exception.OrderServiceException;
import com.sfpay.coreplatform.order.service.proxy.account.IAccountMgrProxyService;
import com.sfpay.coreplatform.order.service.validate.ValidateUtil;
import com.sfpay.coreplatform.order.valueobject.dto.PayAccountDetail;
import com.sfpay.coreplatform.order.valueobject.dto.PayAccountDetailRequest;
import com.sfpay.coreplatform.order.valueobject.dto.PayTradeDetail;
import com.sfpay.framework.base.pagination.IPage;
import com.sfpay.framework.base.pagination.impl.Page;


@Service
public class OrderQueryServiceImpl implements IOrderQueryService {
	

	@Resource
	private IOrderQueryDao orderQueryDao;
	
	@Resource
	private IAccountMgrProxyService accountMgrProxy;
	
	private static Logger logger = LoggerFactory.getLogger(OrderQueryServiceImpl.class);
	
	
	@Override
	public List<PayTradeDetail> queryPayTradeByOutTradeNo(Long platformMemberNo, String outTradeNo) throws OrderServiceException {
		// 参数校验
		ValidateUtil.validateEmpty(outTradeNo, "param outTradeNo is null or empty");
		if(null == platformMemberNo || platformMemberNo == 0L){
			throw new OrderServiceException(OrderResponseCode.ORDER_RESPONSE_PARAM_ISNULL, "param platformMemberNo is null or zero");
		}
		// 获取对象
		List<PayTradeDetail> payTradeDetails;
		try {
			payTradeDetails = orderQueryDao.queryPayTradeByOutTradeNo(platformMemberNo, outTradeNo);
		} catch (Throwable e) {
			logger.error("查询订单单笔交易明细信息异常", e);
			if(SQLException.class.isInstance(e.getCause())){				
				throw new OrderServiceException(OrderResponseCode.ORDER_DB_EXCEPTION, ""+((SQLException)e).getErrorCode(), e);
			}else{
				throw new OrderServiceException(OrderResponseCode.ORDER_DB_EXCEPTION, "db query exception", e);
			}
		}
		return payTradeDetails;
	}
	
	/***
	 * 
	 */
	@Override
	public IPage<PayAccountDetail> queryPayAccountDetails(
			PayAccountDetailRequest req, int pageNo, int pageSize)
			throws OrderServiceException {
		
		if(logger.isDebugEnabled()){
			logger.debug("queryPayAccountDetails begin, request is{" 
						+ "PayAccountDetailRequest=" + req
						+ ", pageNo=" + pageNo
						+ ", pageSize=" + pageSize + "}");
		}
		ValidateUtil.validateNull(req.getAccountNo(), "param accountNo is null");
		ValidateUtil.validateEmpty(req.getBeginDate(), "param beginDate is null or empty");
		ValidateUtil.validateDateFormat1(req.getBeginDate(), "param beginDate format is wrong");
		ValidateUtil.validateEmpty(req.getBeginDate(), "param endDate is null or empty");
		ValidateUtil.validateDateFormat1(req.getEndDate(), "param endDate format is wrong");
		
		TallySerialQuery queryData = new TallySerialQuery();
		queryData.setAccountNo(req.getAccountNo());
		queryData.setStartTallyTime(req.getBeginDate());
		queryData.setEndTallyTime(req.getEndDate());
		IPage<TallySerial> tallyPage = accountMgrProxy.queryTallySerial(queryData, pageNo, pageSize);
		
		IPage<PayAccountDetail> actDetialPage = null;
		if(tallyPage != null){
			List<PayAccountDetail> actDetailList = new ArrayList<PayAccountDetail>(tallyPage.getData().size());
			Iterator it = tallyPage.getData().iterator();
			while(it.hasNext()){
				TallySerial tallySerial = (TallySerial)it.next();
				PayAccountDetail detail = new PayAccountDetail();
				detail.setAmt(tallySerial.getDrcrAmount());
				detail.setOrderNo(tallySerial.getTransOrderNo());
				detail.setBalance(tallySerial.getAccountAmount());
				detail.setCreateTime(tallySerial.getCreateTime());
				detail.setUpdateTime(tallySerial.getUpdateTime());
				actDetailList.add(detail);
			}
			actDetialPage = new Page<PayAccountDetail>(actDetailList, 
					tallyPage.getTotalRecord(), tallyPage.getIndex(), tallyPage.getSize());
			
		}
	
		if(logger.isDebugEnabled()){
			logger.debug("queryPayAccountDetails end, response is " + actDetialPage );
		}
		return actDetialPage;
	}
	

}
